Skip to content

fix: usage-card success rate overflow (成功率保留 2 位小数) - #1

Open
Tony-ooo wants to merge 1 commit into
flyingcoding:mainfrom
Tony-ooo:fix/success-rate-overflow
Open

fix: usage-card success rate overflow (成功率保留 2 位小数)#1
Tony-ooo wants to merge 1 commit into
flyingcoding:mainfrom
Tony-ooo:fix/success-rate-overflow

Conversation

@Tony-ooo

@Tony-ooo Tony-ooo commented Sep 2, 2026

Copy link
Copy Markdown

中文

现象 / Symptom

设置页「账户用量」卡片的成功率一栏显示全精度小数(例如 99.9882890268181%),数字溢出统计格子并超出卡片右缘(在小窗口下尤其明显)。同卡片的其它统计(花费 $34.6038、Token 839.1M 等)均正常。

根因 / Root cause

两处叠加造成:

  1. 成功率是全站唯一没有格式化器的统计。上游 /alpha/usage/summary 返回的 successRate 是全精度浮点数,Host 端 src/adapter.ts 只做 numberValue(...) ?? 0 透传;浏览器端 src/client/section.tsx 直接字符串插值 `${report.usage.successRate}%` —— 没有 toFixed()。同卡片其余三项全部走 formatMoney / formatMoneyExact / formatTokensCompact 格式化。
  2. 统计值的 CSS 没有溢出兜底.cc-usageStat 是 flex column,外层网格轨道 minmax(120px, 1fr);.cc-usageStatValue 没有 min-width: 0 / overflow-wrap,一个 16 字符的连续数字串是不可断行单元,min-content 宽度超过轨道最小宽度后直接横向溢出。

修复 / Fix

  • src/client/usage.ts:新增 formatPercent()(保留 2 位小数,与其它格式化器并列)。
  • src/client/section.tsx:成功率改为 formatPercent(report.usage.successRate) 渲染。
  • src/client/index.ts:.cc-usageStat / .cc-usageStatValue 增加 min-width: 0overflow-wrap: anywhere,任何超长数字回退为换行而不是破框。
  • src/commands.ts:/commandcode 命令行的成功率同步为 toFixed(2),与卡片口径一致。
  • tests/:新增 formatPercent 用例,并更新命令输出的断言。

验证 / Verification

  • npm run typecheck 通过
  • npm test 282/282 通过
  • npm run build 重建产物,已确认 bundle 中不再有裸插值

English

Symptom: The account-usage card's success rate renders the API's full-precision float (e.g. 99.9882890268181%), overflowing the stat tile and the card edge, while every other stat (cost $34.6038, tokens 839.1M, …) renders fine.

Root cause (two compounding layers):

  1. The success rate is the only stat without a display formatter: src/client/section.tsx interpolates `${report.usage.successRate}%` raw, while the neighbouring stats all go through formatMoney / formatTokensCompact.
  2. The stat value has no overflow guard: .cc-usageStatValue lacks min-width: 0 / overflow-wrap, so the 16-char unbreakable number exceeds the minmax(120px, 1fr) grid track and spills out horizontally.

Fix:

  • Add formatPercent() (2 decimals) in src/client/usage.ts and use it in the card.
  • Harden .cc-usageStat / .cc-usageStatValue with min-width: 0 + overflow-wrap: anywhere so any long value wraps instead of overflowing.
  • Round the /commandcode command-line success rate to the same 2 decimals.
  • Tests: new formatPercent cases + updated command-output assertions.

Verification: npm run typecheck clean · npm test 282/282 pass · rebuilt bundle no longer contains the raw interpolation.

The account-usage card rendered the API's full-precision success rate
raw (e.g. 99.9882890268181%), which exceeded the stat tile and pushed
past the card edge. Unlike the neighbouring stats, the success rate had
no display formatter, and the stat value CSS had no overflow guard.

- add formatPercent() (2 decimals) beside the other formatters
- render the success rate through it in the settings-page card
- harden .cc-usageStat/.cc-usageStatValue with min-width:0 and
  overflow-wrap:anywhere so long values wrap instead of overflowing
- apply the same 2-decimal rounding to the /commandcode command line
- cover formatPercent and the updated command output in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant